package com.sztx.pay.center.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.util.Constance.PayFlowType;
import com.sztx.pay.center.common.util.Constance.TradeOrderType;
import com.sztx.pay.center.core.service.BankUtilService;
import com.sztx.pay.center.core.service.PayFlowHandleService;
import com.sztx.pay.center.dataaccess.dao.mysql.FinanceStatisticsMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.GatewayChannelMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderB2cMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRechargeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRefundMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderTransferMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderWithdrawMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowCollectMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserBindCardMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.FinanceStatisticsDO;
import com.sztx.pay.center.dataaccess.domain.OrderB2cDO;
import com.sztx.pay.center.dataaccess.domain.OrderRechargeDO;
import com.sztx.pay.center.dataaccess.domain.OrderRefundDO;
import com.sztx.pay.center.dataaccess.domain.OrderTransferDO;
import com.sztx.pay.center.dataaccess.domain.OrderWithdrawDO;
import com.sztx.pay.center.dataaccess.domain.PayFlowCollectDO;
import com.sztx.pay.center.dataaccess.domain.PayFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserBindCardDO;
import com.sztx.se.common.util.date.DateUtil;
import com.sztx.se.common.util.validate.ValidateUtil;
import com.sztx.se.core.service.impl.BaseServiceImpl;

@Service("payFlowHandleService")
public class PayFlowHandleServiceImpl extends BaseServiceImpl implements PayFlowHandleService {
	
	@Autowired
	private PayFlowMysqlDAO payFlowMysqlDAO;
	@Autowired
	private OrderB2cMysqlDAO orderB2cMysqlDAO;
	@Autowired
	private OrderRechargeMysqlDAO orderRechargeMysqlDAO;
	@Autowired
	private OrderRefundMysqlDAO orderRefundMysqlDAO;
	@Autowired
	private OrderTransferMysqlDAO orderTransferMysqlDAO;
	@Autowired
	private OrderWithdrawMysqlDAO orderWithdrawMysqlDAO;
	@Autowired
	private UserBindCardMysqlDAO userBindCardMysqlDAO;
	
	@Autowired
	private GatewayChannelMysqlDAO gatewayChannelMysqlDAO;
	
	@Autowired
	private PayFlowCollectMysqlDAO payFlowCollectMysqlDAO;
	
	@Autowired
	private FinanceStatisticsMysqlDAO financeStatisticsMysqlDAO;
	@Autowired
	private BankUtilService bankUtilService;
	@Override
	public List<PayFlowDO> findUnHandle(Integer taskItemNum, List<String> taskItems,
			Integer fetchNum) {
		ValidateUtil.notNull(taskItemNum, "taskItemNum不允许为空");
		ValidateUtil.notNull(taskItems, "taskItems不允许为空");
		ValidateUtil.notNull(fetchNum, "fetchNum不允许为空");
		return payFlowMysqlDAO.findUnHandleForSchedule(taskItemNum, taskItems, fetchNum);
	}

	@Override
	public boolean updateHandle(String payFlowNo, List<Integer> oldStatus,
			Integer newStatus, Integer num) {
		ValidateUtil.isNotBlank(payFlowNo, "payFlowNo不允许为空");
		ValidateUtil.notNull(oldStatus, "oldStatus不允许为空");
		ValidateUtil.notNull(newStatus, "newStatus不允许为空");
		ValidateUtil.notNull(num, "num不允许为空");
		int result = payFlowMysqlDAO.updateHandleForSchedule(payFlowNo, oldStatus, newStatus, num);
		return result > 0;
	}

	@Override
	public void addFinance(PayFlowDO payFlowDO) {
		Integer tradeOrderType = payFlowDO.getTradeOrderType();
		Integer payType = payFlowDO.getPayType();
		String originOrderNo = payFlowDO.getOriginOrderNo();
		
		FinanceStatisticsDO financeStatisticsDO = new FinanceStatisticsDO();
		PayFlowCollectDO payFlowCollectDO = new PayFlowCollectDO();
		
		if (tradeOrderType == TradeOrderType.BIND_CARD_ORDER.getCode()) {
			return;
		} else if (tradeOrderType == TradeOrderType.B2C_TRADE_ORDER.getCode()) {
			
			//查询order_b2c
			OrderB2cDO orderB2cDO = orderB2cMysqlDAO.findByIdForSchedule(originOrderNo);
			financeStatisticsDO.setBussinessType(orderB2cDO.getBusinessType());
			payFlowCollectDO.setBussinessType(orderB2cDO.getBusinessType());
			payFlowCollectDO.setFee((long)orderB2cDO.getFee());
		} else if (tradeOrderType == TradeOrderType.CHARGE_ORDER.getCode()) {
			
//			查询order_recharge
			OrderRechargeDO findById = orderRechargeMysqlDAO.findByIdForSchedule(originOrderNo);
			financeStatisticsDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setFee(findById.getRechargeFee());
		} else if (tradeOrderType == TradeOrderType.BALANCE_WITHDRAW_ORDER.getCode() || tradeOrderType == TradeOrderType.PROFIT_WITHDRAW_ORDER.getCode()) {
//			查询order_withdraw
			OrderWithdrawDO findById = orderWithdrawMysqlDAO.findByIdForSchedule(originOrderNo);
			String bindNo = findById.getPayerBindNo();
			UserBindCardDO findByUserCardNo = userBindCardMysqlDAO.findByUserCardNoForSchedule(bindNo);
			if (findByUserCardNo != null) {
				payFlowCollectDO.setBankName(findByUserCardNo.getBankName());
				payFlowCollectDO.setBankNo(findByUserCardNo.getCardNo());
				payFlowCollectDO.setWithDrawType(findByUserCardNo.getBindType());
			}
			financeStatisticsDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setFee(findById.getWithdrawFee());
		} else if (tradeOrderType == TradeOrderType.TRANSFER_ORDER.getCode()) {
			
//			查询order_transfer
			OrderTransferDO findById = orderTransferMysqlDAO.findByIdForSchedule(originOrderNo);
			financeStatisticsDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setFee(findById.getTransferFee());
			if (payType == PayFlowType.DISTRIBUTE.getCode()) {
				
				payFlowCollectDO.setBankName(bankUtilService.queryBankInfo(findById.getPayeeCardNo()).getBankName());
				payFlowCollectDO.setBankNo(findById.getPayeeCardNo());
				payFlowCollectDO.setWithDrawType(2);
			}
		} else if (tradeOrderType == TradeOrderType.REFUND_ORDER.getCode()) {
			
			//查询order_refund
			OrderRefundDO orderRefundDO = orderRefundMysqlDAO.findByIdForSchedule(originOrderNo);
			OrderB2cDO findById = orderB2cMysqlDAO.findByIdForSchedule(orderRefundDO.getSrcOrderId());
			financeStatisticsDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setFee(0l);
		} else if (tradeOrderType == TradeOrderType.SETTLE_ORDER.getCode()) {
			
			//查询order_b2c
			OrderB2cDO findById = orderB2cMysqlDAO.findByIdForSchedule(originOrderNo);
			financeStatisticsDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setBussinessType(findById.getBusinessType());
			payFlowCollectDO.setFee((long)findById.getFee());
		}
		
		if (payType == PayFlowType.PAY.getCode()) {
			financeStatisticsDO.setTradeOrderType(1);
			payFlowCollectDO.setTradeOrderType(1);
		} else if (payType == PayFlowType.TRANSFER.getCode() || payType == PayFlowType.BATCH_TRASFER.getCode()) {
			financeStatisticsDO.setTradeOrderType(2);
			payFlowCollectDO.setTradeOrderType(2);
		} else if (payType == PayFlowType.DISTRIBUTE.getCode()) {
			financeStatisticsDO.setTradeOrderType(4);
			payFlowCollectDO.setTradeOrderType(4);
		} else if (payType == PayFlowType.REFUND.getCode()) {
			financeStatisticsDO.setTradeOrderType(3);
			payFlowCollectDO.setTradeOrderType(3);
		}
		
		payFlowCollectDO.setAmount((long)payFlowDO.getAmount());
		payFlowCollectDO.setConfirmTime(payFlowDO.getModifyTime());
		payFlowCollectDO.setCreateTime(payFlowDO.getCreateTime());
		
		if (payFlowDO.getPayerUserType() == 0) {
			
			payFlowCollectDO.setPayerUserId(StringUtils.isBlank(payFlowDO.getPayerTfActNo())? payFlowDO.getPayerUserId() :payFlowDO.getPayerTfActNo());
			financeStatisticsDO.setPayerUserId(StringUtils.isBlank(payFlowDO.getPayerTfActNo())? payFlowDO.getPayerUserId() :payFlowDO.getPayerTfActNo());
			if ((!"000001".equals(payFlowDO.getPayChannelId())) || payFlowDO.getPayChannelId() == null){
				payFlowCollectDO.setPayerUserId("000000");
				financeStatisticsDO.setPayerUserId("000000");
			}
		} else {
			payFlowCollectDO.setPayerUserId(payFlowDO.getPayerUserId());
			financeStatisticsDO.setPayerUserId(payFlowDO.getPayerUserId());
		}
		
		if (payFlowDO.getPayeeUserType() == 0) {
			payFlowCollectDO.setPayeeUserId(StringUtils.isBlank(payFlowDO.getPayeeTfActNo())? payFlowDO.getPayeeUserId() :payFlowDO.getPayeeTfActNo());
			financeStatisticsDO.setPayeeUserId(StringUtils.isBlank(payFlowDO.getPayeeTfActNo())? payFlowDO.getPayeeUserId() :payFlowDO.getPayeeTfActNo());
			if ((!"000001".equals(payFlowDO.getPayChannelId())) || payFlowDO.getPayChannelId() == null){
				payFlowCollectDO.setPayeeUserId("000000");
				financeStatisticsDO.setPayeeUserId("000000");
			}
		} else {
			payFlowCollectDO.setPayeeUserId(payFlowDO.getPayeeUserId());
			financeStatisticsDO.setPayeeUserId(payFlowDO.getPayeeUserId());
		}
		
		payFlowCollectDO.setPayerTfActNo(StringUtils.isBlank(payFlowDO.getPayerTfActNo())? payFlowDO.getPayerUserId() :payFlowDO.getPayerTfActNo());
		payFlowCollectDO.setPayeeTfActNo(StringUtils.isBlank(payFlowDO.getPayeeTfActNo())? payFlowDO.getPayeeUserId() :payFlowDO.getPayeeTfActNo());
		
		payFlowCollectDO.setPayFlowNo(payFlowDO.getPayFlowNo());
		if (payType == PayFlowType.TRANSFER.getCode() || payType == PayFlowType.BATCH_TRASFER.getCode()) {
			payFlowCollectDO.setPayType("000001");
			financeStatisticsDO.setPayType("000001");
		} else {
			payFlowCollectDO.setPayType(payFlowDO.getPayChannelId());
			financeStatisticsDO.setPayType(payFlowDO.getPayChannelId());
		}
		payFlowCollectDO.setRemark(payFlowDO.getRemark());
		payFlowCollectDO.setGateWayNo(payFlowDO.getGatewayOrderNo());
		payFlowCollectDO.setTradeOrderStatus(payFlowDO.getPayStatus());
		payFlowCollectDO.setTradeOrderNo(originOrderNo);
		
		financeStatisticsDO.setTradeTime(DateUtil.parse(DateUtil.formatDate(payFlowDO.getModifyTime(), "yyyy-MM-dd"), "yyyy-MM-dd"));
		
		
		financeStatisticsDO.setTradeOrderStatus(payFlowDO.getPayStatus());
		FinanceStatisticsDO selectFinanceDo = null;
		try {
			selectFinanceDo = financeStatisticsMysqlDAO.findUnRepeatForSchedule(financeStatisticsDO , null);
		} catch (Exception e) {
			LOG.info("financeStatisticsDO:" + financeStatisticsDO);
			throw e;
		}
		if (selectFinanceDo == null){
			financeStatisticsDO.setTotalAmount((long)payFlowDO.getAmount());
			financeStatisticsDO.setTotalCount(1);
			financeStatisticsMysqlDAO.saveOrUpdateForSchedule(financeStatisticsDO);
		} else {
			financeStatisticsMysqlDAO.updateInfoForSchedule((long) payFlowDO.getAmount(), selectFinanceDo.getId());
		}
		
		payFlowCollectMysqlDAO.saveForSchedule(payFlowCollectDO);
	}

	@Override
	public boolean batchUpdateHandle(PayFlowDO[] payFlowNos, Integer oldStatus,
			Integer newStatus, Integer num) {
		List<String> taskItemIds = new ArrayList<String>();
		for (int i = 0; i < payFlowNos.length; i++) {
			taskItemIds.add(payFlowNos[i].getPayFlowNo());
		}
		payFlowMysqlDAO.batchUpdateHandleForSchedule(taskItemIds, oldStatus, newStatus, num);
		return false;
	}

}
